<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mend.class.php 2011-07-06 13:59:48 $
 * @Show Note: 设备维修
 */
Class zy_mend extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_mend()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_mend.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_mend");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		//$this->smarty->assign('admin_info',get_admin_info());//取得管理员用户数组
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_mend();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "mc_mend_id";
		$where = 1;
		//维修单号检索
		$mc_mend_no = isset($_GET['mc_mend_no']) ? trim($_GET['mc_mend_no']) : '';
		if (!empty($mc_mend_no)) {
			$where .= " AND mc_mend_no = '{$mc_mend_no}' ";
		}
		//申请人检索
		$admin_user_id = isset($_GET['admin_user_id']) ? trim($_GET['admin_user_id']) : '';
		if (!empty($admin_user_id)) {
			$where .= " AND admin_user_id = {$admin_user_id} ";
		}
		//经办日期
		$mc_mend_time_start = isset($_GET['mc_mend_time_start']) && !empty($_GET['mc_mend_time_start']) ? strtotime(($_GET['mc_mend_time_start'])) : '';
		$mc_mend_time_end = isset($_GET['mc_mend_time_end']) && !empty($_GET['mc_mend_time_end']) ? strtotime($_GET['mc_mend_time_end']) : '';
		
		if (!empty($mc_mend_time_start)) {
			$where .= " AND mc_mend_time >= {$mc_mend_time_start} ";
		}
		
		if (!empty($mc_mend_time_end)) {
			$mc_mend_time_end = $mc_mend_time_end + 86400 - 1 ;//多加一天
			$where .= " AND mc_mend_time <= {$mc_mend_time_end} ";
		}
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		//维修记录展示数组
		/*$zy_mend_log_arr = array();
		foreach ($arr as $key=>$value)
		{
			$zy_mend_log_arr[$value['mc_mend_id']] = $this->get_mend_by_id($value['mc_mend_id']);//取得维修ID对应的维修记录信息
		}*/
		$this->smarty->assign('arr',$arr);
		/*$this->smarty->assign('zy_mend_log_arr',$zy_mend_log_arr);//维修记录*/
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_mend($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, 'index.php?c=zy_mend');
			}else{
				redirectPage('index.php?c=zy_mend');
			}
			//alertMsg($error_no, 'index.php?c=zy_mend');
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$mc_mend_id = isset($_REQUEST['mc_mend_id']) ? intval(trim($_REQUEST['mc_mend_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($mc_mend_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_mend');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "mc_mend_id=$mc_mend_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_mend($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, 'index.php?c=zy_mend');
			}else{
				redirectPage('index.php?c=zy_mend');
			}
			//alertMsg($error_no, 'index.php?c=zy_mend');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$mc_mend_id = isset($_GET['mc_mend_id']) ? intval($_GET['mc_mend_id']) : 0;
		//使用模版，展示提交界面
		$result = $this->db->getOne($mc_mend_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_mend');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		$zy_mend_log_arr = array();
		$zy_mend_log_arr = $this->get_mend_by_id($mc_mend_id);//取得维修ID对应的维修记录信息
		//分页，通过查询所有来去
		$rowNo = count($zy_mend_log_arr);
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		//unset($zy_plan_reply_mc_arr);
		for ($i=$offset;$i<($numrows+$offset);$i++){
			if(isset($zy_mend_log_arr[$i]))
			$page_arr[] = $zy_mend_log_arr[$i];
		}
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		
		$this->smarty->assign('zy_mend_log_arr',$page_arr);//维修记录
		
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$mc_mend_id = isset($_REQUEST['mc_mend_id']) ? intval(trim($_REQUEST['mc_mend_id'])) : 0 ;
		//判断该ID下面是否存在维修记录
		$temp =  $this->get_mend_by_id($mc_mend_id);
		if (!empty($temp)) {
			alertMsg('-2051');
		}
		$where = "mc_mend_id=$mc_mend_id";
		$del=$this->db->delete_zy_mend($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, 'index.php?c=zy_mend');
		}else{
			redirectPage('index.php?c=zy_mend');
		}
		//alertMsg($error_no, 'index.php?c=zy_mend');
	}

	/**
	 *	import method.
	 */
	public function imports()
	{
		$opt = isset($_POST['opt']) ? intval($_POST['opt']) : 0 ;
		$id = isset($_REQUEST['mc_mend_id']) ? intval($_REQUEST['mc_mend_id']) : 0 ;
		if($id <= 0) {
			alertMsg2("操作有误", 'index.php?c=zy_mend');
		}
		if(empty($opt))	{
			//获取维修单内容
			$mendInfo = $this->db->getOne($id);
			$mc_mend_no = isset($mendInfo['mc_mend_no']) ? trim($mendInfo['mc_mend_no']) : '';

			$this->smarty->assign('act_title', '导入数据');
			$this->smarty->assign('act', 'imports');
			$this->smarty->assign('control', 'zy_mend');
			$this->smarty->assign('mc_mend_id', $id);
			$this->smarty->assign('mc_mend_no', $mc_mend_no); //维修单号
			$this->smarty->display($this->tplFile);
		} else {
			//1. 上传xls
			$savePath = UPLOAD_PATH . 'xls/';
			$uploadRes = $this->upload_file_xls($savePath);
			if(0 == $uploadRes['state']) {
				alertMsg2($uploadRes['msg'], 'index.php?c=zy_mend');
			}
			$fileName = $uploadRes['msg']; //上传成功后文件名

			//2. 上传成功后读取xls数据分析
			$item_pe = Factory::getItemClass('parse_excel');

			//获取xls文件数据
			$filePath = UPLOAD_PATH . "xls/".$fileName;
			$arr_xls = $item_pe->data_xls($filePath);

			$out = $item_pe->data_parse_xls($arr_xls);
			//dump($out);exit;
			if(1 != $out['state']) {
				alertMsg2($out['msg'], 'index.php?c=zy_mend');
			}

			//3. 入库
			$db_zml = Factory::getDB('zy_mend_log');
			foreach($out['list'] as $k => $v) {
				if(!empty($v)) {
					$val['mc_mend_id'] = $id; //维修ID
					$val['admin_user_id'] = $this->admin_user_id; //录入用户：后台登录用户
					if(!empty($v[0]) && !empty($v[1])) {
						//验证是否已插入

						$val['mc_no'] = $v[0];
						$val['mc_name'] = $v[1];
						$val['mc_model'] = isset($v[2]) ? trim($v[2]) : ''; //型号
						$val['mc_number'] = isset($v[3]) ? trim($v[3]) : ''; //数量
						$val['mc_mend_cost'] = isset($v[4]) ? trim($v[4]) : ''; //修理费用
						$val['mend_comp'] = isset($v[5]) ? trim($v[5]) : ''; //修理单位
						
						$resId = $db_zml->insert_zy_mend_log($val);
					}
				}
			}
			//redirectPage('index.php?c=zy_mend');
			alertMsg2('导入完成', 'index.php?c=zy_mend');
		}
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		$temp['mc_mend_no'] = isset($_POST['mc_mend_no']) ? htmlspecialchars(trim($_POST['mc_mend_no'])) : '';//维修单号
		if (empty($temp['mc_mend_no'])) {
			alertMsg('-2044');
		}
		if($isEdit){
			$temp['mc_mend_id'] = isset($_POST['mc_mend_id']) ? intval($_POST['mc_mend_id']) : 0;
			if (empty($temp['mc_mend_id'])) {
				alertMsg('-2047');
			}
			//查重
			if ($this->check_is_exist($temp['mc_mend_no'],$temp['mc_mend_id'])) {
				alertMsg('-2060');
			}
		} else {
			$temp['mc_mend_id'] = "";
			//查重
			if ($this->check_is_exist($temp['mc_mend_no'])) {
				alertMsg('-2060');
			}
		}
		//补充其他的字段
		$temp['mend_info'] = $_POST['mend_info'];
		$temp['mc_mend_time'] = isset($_POST['mc_mend_time']) && !empty($_POST['mc_mend_time']) ? strtotime($_POST['mc_mend_time']) : 0;//经办日期
		if (empty($temp['mc_mend_time'])) {
			alertMsg('-2045');
		}
		$temp['admin_user_id'] = isset($_POST['admin_user_id']) ? intval($_POST['admin_user_id']) : 0;//经办人（选择后台用户）
		if (empty($temp['admin_user_id'])) {
			alertMsg('-2046');
		}
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		//判断是否存在文件上传
		if (!empty($file_path['name']) && !empty($temp['mc_mend_no'])) {
			$re = upload_file($_FILES,'file_path',$temp['mc_mend_no']);
			$temp['file_path'] = $re;//获得上传文件名称
		}
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'设备维修'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				//'imports'  => array('imports','导入',32,false),
				'updateFile' => array('updateFile','文件上传',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
	/**
	 * 根据维修ID取得维修记录内容
	 *
	 * @param unknown_type $mend_id
	 */
	function get_mend_by_id($mend_id)
	{
		$zy_mend_log_db = Factory::getDB('zy_mend_log');
		//取得维修记录信息
		$temp = $zy_mend_log_db->get_memd_log_info($mend_id);
		return is_array($temp) ? $temp : array();
	}
	/**
	 * 查看维修单号是否重复
	 *
	 * @param unknown_type $mc_mend_no
	 * @return unknown
	 */
	function check_is_exist($mc_mend_no,$mc_mend_id='')
	{
		$zy_mend_db = Factory::getDB('zy_mend');
		return $zy_mend_db->check_is_exist($mc_mend_no,$mc_mend_id);
	}

	/**
	 * (功能描述) 文件上传
	 *
	 * @param 
	 * @return
	 */
	function updateFile() {
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		$mc_mend_id = isset($_REQUEST['mc_mend_id']) ? intval(trim($_REQUEST['mc_mend_id'])) : 0 ;
		$error_no = 0;
		//判断是否存在文件上传
		if (!empty($file_path['name'])) {
			$array = array(
				'source_file_name'=>$file_path['name'],
			);
			$file_name = upload_file($_FILES,'file_path',md5($file_path['name'].time()),$_GET['c']);
			$array['file_path'] = $file_name;
			$where = "mc_mend_id = '{$mc_mend_id}' ";
			$update = $this->db->update_zy_mend($array,$where);
			$error_no = 0 > $update ?  $update :'-1502';
		}
		if ($error_no == '-1502') {
			echo json_encode(array('code'=>1));exit;
		}else{
			echo json_encode(array('code'=>2));exit;
		}
	}
}

?>